<template>
  <div class="content">
    <div class="header">
      <h3 class="title">部门列表</h3>
      <el-button type="primary" @click="newClick">新建部门</el-button>
    </div>
    <div class="table">
      <el-table :data="pageList" border style="width: 100%">
        <el-table-column align="center" type="selection" width="50px" />
        <el-table-column align="center" type="index" label="序号" width="60px" />

        <el-table-column align="center" label="部门名称" prop="name" width="150px" />
        <el-table-column align="center" label="部门领导" prop="leader" width="150px" />
        <el-table-column align="center" label="上级部门" prop="parentId" width="150px" />

        <el-table-column align="center" label="创建时间" prop="createAt">
          <template #default="scope">
            {{ formatUTC(scope.row.createAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="更新时间" prop="updateAt">
          <template #default="scope">
            {{ formatUTC(scope.row.updateAt) }}
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作" width="150px">
          <template #default="scope">
            <el-button
              size="small"
              icon="Edit"
              type="primary"
              text
              @click="handleEditBtnClick(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              size="small"
              icon="Delete"
              type="danger"
              text
              @click="handleDeleteBtnClick(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30]"
        size="small"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { storeToRefs } from 'pinia'
import useSystemStore from '@/store/system/system'
import { formatUTC } from '@/utils/format'

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)

// 发起网络请求，获取数据
const systemStore = useSystemStore()
fetchPageListData()
// 如果通过这种结构的方式，结果在第一次是不会显示的，因为发送网络请求和解构是异步的
// 要将 systemStore 转换为响应式，通过 storeToRefs
// const { usersList, usersTotalCount } = systemStore
const { pageList, pageTotalCount } = storeToRefs(systemStore)

// 请求条数的改变
function handleSizeChange() {
  fetchPageListData()
}
// 当前页码的改变
function handleCurrentChange() {
  fetchPageListData()
}

// 网络请求相关 any = {} 默认为空对象
function fetchPageListData(formData: any = {}) {
  // 获取 size展示多少条数据 offset偏移量
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const pageInfo = { size, offset }

  const queryInfos = { ...formData, ...pageInfo }

  // 发送网络请求
  systemStore.postPageListAction('department', queryInfos)
}
// 向外暴露 网络请求函数
defineExpose({ fetchPageListData })

// 删除/新建/编辑的操作
const emit = defineEmits(['newClick', 'editClick'])
// 创建用户函数,向外暴露 click 事件
function newClick() {
  emit('newClick')
}
function handleDeleteBtnClick(id: number) {
  systemStore.deletePageByIdAction('department', id)
}
function handleEditBtnClick(itemData: any) {
  // console.log(itemData)
  emit('editClick', itemData)
}
</script>

<style lang="less" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background-color: #fff;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-bottom: 10px;

  .title {
    font-size: 22px;
  }
}
.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }
  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  :deep(.el-input__inner) {
    user-select: none;
  }
}
</style>
